package com.salim.leetcode.$8;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-9-14
 * Time: 下午2:22
 * To change this template use File | Settings | File Templates.
 */
public class StringToInteger {
    public int myAtoi(String str) {
        int result = 0;
        String tempStr = str.trim();
        int negative = 1;
        boolean plusminus = false;
        boolean inNumber = false;
        while (tempStr.length()>0&&(tempStr.charAt(0)<49||tempStr.charAt(0)>57)){
            int temp = tempStr.charAt(0);
            if(temp==48){
                inNumber=true;
            }else if(inNumber){
                return 0;
            }else if(temp==43){
                inNumber=true;
            }else if(temp==45){
                negative=-1;
                inNumber=true;
            }else if(temp==32){

            }else{
                return 0;
            }

            tempStr=tempStr.substring(1,tempStr.length());
        }

        int count = 0;
        for(int i=0;i<tempStr.length();i++){
            int temp = tempStr.charAt(i);
            if(temp>47&&temp<58){
                count++;
            }else{
                break;
            }
        }
        int tempResult=0;
        for(int i=0;i<count;i++){
            int temp = tempStr.charAt(i);
            int number = temp-48;
            int tempCount = count-i;
            int tempnumber = 0;
            while(tempCount-1>0){
                number*=10;
                //越界
                if(tempnumber!=0&& number/tempnumber!=10){
                    if(negative==1){
                        return Integer.MAX_VALUE;
                    }else{
                        return Integer.MIN_VALUE;
                    }
                }
                tempnumber = number;
                tempCount--;
            }
            result+=number;
            if(tempResult>result){
                if(negative==1){
                    return Integer.MAX_VALUE;
                }else{
                    return Integer.MIN_VALUE;
                }
            }
            tempResult=result;
        }
        result*=negative;
        return result;
    }

    public static void main(String[] args){
        StringToInteger sti = new StringToInteger();
/*        String $12345 = "12345";
        System.out.println(sti.myAtoi($12345));*/
/*        String $12345$12345 = "12345.12345";
        System.out.println(sti.myAtoi($12345$12345));
        String $$12345$12345 = "-12345.12345";
        System.out.println(sti.myAtoi($$12345$12345));
        String $$1 = "+1";
        System.out.println(sti.myAtoi($$1));
        String $21312 = "    +0a32";
        System.out.println(sti.myAtoi($21312));
        String $intmax = "2147483648";
        System.out.println(sti.myAtoi($intmax));
        String $intmin = "-2147483649";
        System.out.println(sti.myAtoi($intmin));
        String $intspace = "   - 321";
        System.out.println(sti.myAtoi($intspace));
        String $b11111= " b11228552307";
        System.out.println(sti.myAtoi($b11111));*/
        String max = "    -10522545459";
        System.out.println(sti.myAtoi(max));
    }
}
